#include "PlayingInfoViewer.h"
#include "../Controllers/DotController.h"
#include "../Models/DotModel.h"
#include "../Settings/GameSettings.h"
#include <QPainter>
#include <QTextLayout>
#include <QLabel>

PlayingInfoViewer::PlayingInfoViewer(DotController *pController,QWidget *parent) :
    QWidget(parent),mpController(pController)
{
    QFont serifFont("Times", 15, QFont::Bold);
    this->setFont(serifFont);
    this->setAutoFillBackground(true);
    connect(GameSettings::me(),SIGNAL(playerNameChanged(Game::Player)),SLOT(playerNameChanged(Game::Player)));
    connect(GameSettings::me(),SIGNAL(playerColorChanged(Game::Player)),SLOT(playerColorChanged(Game::Player)));
    connect(mpController,SIGNAL(gameStarted()),SLOT(gameStarted()));
    connect(mpController,SIGNAL(cellOwned(Game::Player)),SLOT(cellOwned(Game::Player)));
    connect(mpController,SIGNAL(gameTurnChanged(Game::Player)),SLOT(gameTurnChanged(Game::Player)));
    connect(mpController,SIGNAL(gameFinished(Game::Player)),SLOT(gameFinished(Game::Player)));
    connect(mpController,SIGNAL(gameCanceled()),SLOT(gameCancelled()));
    connect(&mBlinkTimer,SIGNAL(timeout()),SLOT(blink()));
    
    mBlinkTimer.setInterval(500);
    mpLblPlayer1 = new QLabel("Player1",this);
    //mpLblPlayer1->setAutoFillBackground(true);
    mpLblPlayer1->setAlignment(Qt::AlignLeft | Qt::AlignBottom);
    mpLblPlayer2 = new QLabel("Player2",this);
    //mpLblPlayer2->setAutoFillBackground(true);
    mpLblPlayer2->setAlignment(Qt::AlignRight | Qt::AlignBottom);
    showLbls(false);
}

void PlayingInfoViewer::paintEvent(QPaintEvent *pEvent)
{
    QRect r = this->rect();
    QPainter dc;
    dc.begin(this);
    QColor color = GameSettings::me()->setting(Game::ProgressBar).value<QColor>();
    QColor bg(221,221,221);
    dc.fillRect(r,QColor(138,105,6));
    r = r.adjusted(1,1,-1,-1);
    dc.fillRect(r,bg);

    if(mpController->isGameRunning()){
        int nMidWall = 5;
        int nLength = r.width() / 12;
        QRect rp1(nLength,0,r.width()/2 - nLength - nMidWall ,r.height());
        QRect rp2(r.width() / 2 + nMidWall,0,r.width()/2 - nLength - nMidWall ,r.height());
        int nProgress = r.width() * mpController->model()->lines()->count() / mpController->model()->totalLines();
        QRect rctProgress(r.topLeft(),QSize(nProgress,r.height()));
        color.setAlpha(70);
        dc.fillRect(rctProgress,color);
        
        mpLblPlayer1->setFixedWidth(rp1.width());
        mpLblPlayer1->move(rp1.topLeft());
        mpLblPlayer2->setFixedWidth(rp2.width());
        mpLblPlayer2->move(rp2.topLeft());

    }
    else if(mpController->isGameFinished()){
        Game::Player winner = mpController->winner();
        QString strWinInfo = winner != Game::Unknown ?
                             (QString("Game finished and the WINNER is ") +
                              GameSettings::me()->playerName(winner) + QString(" (") +
                              QString::number(mpController->model()->playerCellsCount(winner)) + QString("/") +
                              QString::number(mpController->model()->totalCells()) +QString(")") ) : QString("Game Drawn!!!");
        dc.drawText(r,Qt::TextSingleLine | Qt::AlignCenter,strWinInfo);
    }
    else{
        dc.drawText(r,Qt::TextSingleLine | Qt::AlignCenter,"Players are idle");
    }

    dc.end();
}

void PlayingInfoViewer::gameStarted()
{
    this->setToolTip("A play is under running");
    update();
    updatePlayerInfo();
    mBlinkTimer.start();
    showLbls(true);
}

void PlayingInfoViewer::gameCancelled()
{
    this->setToolTip("Click on 'New Game' button to play a game");
    update();
    mBlinkTimer.stop();
    showLbls(false);
}

void PlayingInfoViewer::gameFinished(Game::Player winner)
{
    this->setToolTip("Click on 'New Game' button to play a game");
    mBlinkTimer.stop();
    showLbls(false);
    update();
}

void PlayingInfoViewer::playerColorChanged(Game::Player player)
{
    updateLblPalatte();
}

void PlayingInfoViewer::playerNameChanged(Game::Player player)
{
    updateLblTxt();
}

void PlayingInfoViewer::gameTurnChanged(Game::Player player)
{
    blink();
}

void PlayingInfoViewer::cellOwned(Game::Player owner)
{
    int nTotalCell = mpController->model()->totalCells();
    int nPlayer1Cell = mpController->model()->playerCellsCount(Game::Player1);
    int nPlayer2Cell = mpController->model()->playerCellsCount(Game::Player2);
    this->setToolTip("Game played " + QString::number(100 * (nPlayer1Cell + nPlayer2Cell) / nTotalCell) + "%");
    updateLblTxt();
    update();
}

void PlayingInfoViewer::lineAdded(Game::Player owner)
{

}

void PlayingInfoViewer::updateLblPalatte()
{
    QPalette pallete1;
    pallete1.setColor(mpLblPlayer1->foregroundRole(),GameSettings::me()->playerColor(Game::Player1));
    mpLblPlayer1->setPalette(pallete1);

    QPalette pallete2;
    pallete2.setColor(mpLblPlayer2->foregroundRole(),GameSettings::me()->playerColor(Game::Player2));
    mpLblPlayer2->setPalette(pallete2);
}

void PlayingInfoViewer::updateLblTxt()
{
    if(mpLblPlayer1->isVisible()){
        QString strPlayer1Info = GameSettings::me()->playerName(Game::Player1);
        strPlayer1Info += (QString(": ") + QString::number(mpController->model()->playerCellsCount(Game::Player1)));
        mpLblPlayer1->setText(mpController->isGameRunning() || mpController->isGameFinished() ? strPlayer1Info : "");
        mpLblPlayer1->show();
    }

    if(mpLblPlayer2->isVisible()){
        QString strPlayer2Info = GameSettings::me()->playerName(Game::Player2);
        strPlayer2Info += (QString(": ") + QString::number(mpController->model()->playerCellsCount(Game::Player2)));
        mpLblPlayer2->setText(mpController->isGameRunning() || mpController->isGameFinished() ? strPlayer2Info : "" );
    }
}

void PlayingInfoViewer::updatePlayerInfo()
{
    updateLblPalatte();
    updateLblTxt();
}

void PlayingInfoViewer::showLbls(bool bShow)
{
    mpLblPlayer1->setVisible(bShow);
    mpLblPlayer2->setVisible(bShow);

    if(bShow){
        updateLblTxt();
    }
}

void PlayingInfoViewer::blink()
{
    static bool bFlip = false;
    updateLblPalatte();
    bFlip = !bFlip;

    if(bFlip){
        Game::Player player = mpController->gameTurner();
        QLabel *mpLable = player == Game::Player1 ? mpLblPlayer1 : mpLblPlayer2;
        QPalette pallete1;
        pallete1.setColor(mpLable->foregroundRole(),QColor(0,100,0));
        mpLable->setPalette(pallete1);
    }
}
